package com.fsl.springbootdesignpattern.e_observer;

import java.util.ArrayList;
import java.util.List;

public abstract class UnderObserver {

    /**
     * 观察者集合
     */
    private List<Observer> observerList = new ArrayList<>();


    public synchronized void attach(Observer observer){
        boolean exist = false;
        for(Observer obs:observerList){
            if(obs.getName().equals(observer.getName())){
                exist = true;
            }
        }
        if(!exist){
            observerList.add(observer);
        }
        System.out.println("新增了一个观察者,现在观察者的数量：" + observerList.size());
    }


    public synchronized void detach(Observer observer){
        for(Observer obs:observerList){
            if(obs.getName().equals(observer.getName())){
                observerList.remove(observer);
                break;
            }
        }
    }

    public void notifyAllObserver(Integer level){
        for(Observer obs:observerList){
            obs.getNotice(level);
        }

    }
}
